package io.plus.engine.pulse.collections;

import io.plus.engine.pulse.Configuration;

import java.util.Collection;

public class IpBlackLists {
    static IpBlackList ipBlackList = new IpBlackList(null);

    /**
     * 把ip添加到黑名单，此黑名单只针对tioConfig有效，其它tioConfig不会把这个ip视为黑名单
     */
    public static boolean add(Configuration configuration, String ip) {
        return configuration.ipBlacklist.add(ip);
    }

    /**
     * 添加全局ip黑名单
     */
    public static boolean add(String ip) {
        return ipBlackList.add(ip);
    }

    /**
     * 清空黑名单，只针对tioConfig有效
     */
    public static void clear(Configuration configuration) {
        configuration.ipBlacklist.clear();
    }

    /**
     * 清空全局黑名单
     */
    public static void clear() {
        ipBlackList.clear();
    }

    /**
     * 获取ip黑名单列表
     */
    public static Collection<String> getAll(Configuration configuration) {
        return configuration.ipBlacklist.getAll();
    }

    /**
     * 获取全局黑名单
     */
    public static Collection<String> getAll() {
        return ipBlackList.getAll();
    }

    /**
     * 是否在黑名单中
     */
    public static boolean isInBlacklist(Configuration configuration, String ip) {
        return configuration.ipBlacklist.isInBlacklist(ip) || ipBlackList.isInBlacklist(ip);
    }

    /**
     * 把ip从黑名单中删除
     */
    public static void remove(Configuration configuration, String ip) {
        configuration.ipBlacklist.remove(ip);
    }

    /**
     * 删除全局黑名单
     */
    public static void remove(String ip) {
        ipBlackList.remove(ip);
    }
}
